package cn.iocoder.yudao.module.erp.controller.admin.purchase;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.ErpProductRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.supplier.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpSupplierDO;
import cn.iocoder.yudao.module.erp.service.purchase.ErpSupplierService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "管理后台 - ERP 供应商")
@RestController
@RequestMapping("/erp/supplier")
@Validated
public class ErpSupplierController {

    @Resource
    private ErpSupplierService supplierService;

    @PostMapping("/create")
    @Operation(summary = "创建供应商")
    @PreAuthorize("@ss.hasPermission('spd:supplier:create')")
    public CommonResult<Long> createSupplier(@Valid @RequestBody ErpSupplierSaveReqVO createReqVO) {
        return success(supplierService.createSupplier(createReqVO));
    }



    @PutMapping("/update")
    @Operation(summary = "更新供应商")
    @PreAuthorize("@ss.hasPermission('spd:supplier:update')")
    public CommonResult<Boolean> updateSupplier(@Valid @RequestBody ErpSupplierSaveReqVO updateReqVO) {
        supplierService.updateSupplier(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除供应商")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('spd:supplier:delete')")
    public CommonResult<Boolean> deleteSupplier(@RequestParam("id") Long id) {
        supplierService.deleteSupplier(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得供应商")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('spd:supplier:query')")
    public CommonResult<ErpSupplierRespVO> getSupplier(@RequestParam("id") Long id) {
        ErpSupplierDO supplier = supplierService.getSupplier(id);
        return success(BeanUtils.toBean(supplier, ErpSupplierRespVO.class));
    }
    @GetMapping("/getSupplierByProductId")
    @Operation(summary = "获得供应商")
    @Parameter(name = "productId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('spd:supplier:query')")
    public CommonResult<List<ErpSupplierRespVO>> getSupplierByProductId(@RequestParam("productId") Long productId) {
        List<ErpSupplierDO> supplier = supplierService.getSupplierByProductId(productId);
        return success(BeanUtils.toBean(supplier, ErpSupplierRespVO.class));
    }
    @GetMapping("/pages")
    @Operation(summary = "获得供应商分页")
    @PreAuthorize("@ss.hasPermission('spd:supplier:query')")
    public CommonResult<PageResult<ErpSupplierRespVO>> getSupplierPages(@Valid ErpSupplierPageReqVO pageReqVO) {
        PageResult<ErpSupplierDO> pageResult = supplierService.getSupplierPages(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ErpSupplierRespVO.class));
    }


    @GetMapping("/page")
    @Operation(summary = "获得供应商分页")
    @PreAuthorize("@ss.hasPermission('spd:supplier:query')")
    public CommonResult<PageResult<ErpSupplierRespVO>> getSupplierPage(@Valid ErpSupplierPageReqVO pageReqVO) {
        PageResult<ErpSupplierDO> pageResult = supplierService.getSupplierPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ErpSupplierRespVO.class));
    }

    @GetMapping("/verifyPage")
    @Operation(summary = "获得供应商待审核分页")
    @PreAuthorize("@ss.hasPermission('spd:supplier:query')")
    public CommonResult<PageResult<ErpSupplierRespVO>> getSupplierVerifyPpagePage(@Valid ErpSupplierPageReqVO pageReqVO) {
        PageResult<ErpSupplierDO> pageResult = supplierService.getSupplierRegisterVerifyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ErpSupplierRespVO.class));
    }

    @GetMapping("/registerPage")
    @Operation(summary = "获得供应商注册信息分页")
    @PermitAll
    public CommonResult<PageResult<ErpSupplierRespVO>> registerPage(@Valid SupplierRegisterPageReqVO pageReqVO) {
        PageResult<ErpSupplierDO> pageResult = supplierService.getSupplierRegisterPage(BeanUtils.toBean(pageReqVO,ErpSupplierPageReqVO.class));
        return success(BeanUtils.toBean(pageResult, ErpSupplierRespVO.class));
    }

    @GetMapping("/getRegister")
    @Operation(summary = "获得供应商")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PermitAll
    public CommonResult<ErpSupplierRespVO> getRegister(@RequestParam("id") Long id) {
        ErpSupplierDO supplier = supplierService.getSupplier(id);
        return success(BeanUtils.toBean(supplier, ErpSupplierRespVO.class));
    }
    @GetMapping("/simple-list")
    @Operation(summary = "获得供应商精简列表", description = "只包含被开启的供应商，主要用于前端的下拉选项")
    public CommonResult<List<ErpSupplierRespVO>> getSupplierSimpleList() {
        List<ErpSupplierDO> list = supplierService.getSupplierListByStatus(CommonStatusEnum.ENABLE.getStatus());
        return success(convertList(list, supplier -> new ErpSupplierRespVO().setId(supplier.getId()).setName(supplier.getName())
                .setFullPinyin(supplier.getFullPinyin()).setFirstPinyin(supplier.getFirstPinyin()).setWubiShortcode(supplier.getWubiShortcode())));
    }


    /**
     * 产品名称筛选框查询方法
     * @param params
     * @return
     */
    @GetMapping("/simple-lists")
    @Operation(summary = "获得产品精简列表", description = "只包含被开启的产品，主要用于前端的下拉选项")
    public CommonResult<List<ErpProductRespVO>> getProductSimpleLists(@RequestParam("params") String params) {
        List<ErpProductRespVO> list = supplierService.getSupplierLists(CommonStatusEnum.ENABLE.getStatus(),params);
        return success(convertList(list, product -> new ErpProductRespVO().setId(product.getId())
                .setName(product.getName())
                .setPurchasePrice(product.getPurchasePrice()).setSalePrice(product.getSalePrice()).setMinPrice(product.getMinPrice())
                .setFullPinyin(product.getFullPinyin()).setFirstPinyin(product.getFirstPinyin()).setWubiShortcode(product.getWubiShortcode())
        ));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出供应商 Excel")
    @PreAuthorize("@ss.hasPermission('spd:supplier:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSupplierExcel(@Valid ErpSupplierPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpSupplierDO> list = supplierService.getSupplierPages(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "供应商.xls", "数据", ErpSupplierRespVO.class,
                        BeanUtils.toBean(list, ErpSupplierRespVO.class));
    }
    @PermitAll
    @PostMapping("/register")
    @Operation(summary = "供应商注册")
    public CommonResult<Long> whiteCreateSupplier(@Valid @RequestBody ErpWhiteSupplierSaveReqVO createReqVO) {
        return success(supplierService.whiteCreateSupplier(createReqVO));
    }
    @PutMapping("/registerUpdate")
    @Operation(summary = "更新供应商注册信息")
    @PermitAll
    public CommonResult<Boolean> registerUpdate(@Valid @RequestBody ErpWhiteSupplierSaveReqVO updateReqVO) {

        supplierService.updateSupplier(BeanUtils.toBean(updateReqVO, ErpSupplierSaveReqVO.class));
        return success(true);
    }
}